<?php

require('config.php');
if(!isset($_SESSION['id_user']))
    redirectFront('connexion');


if(isset($_GET['title_debug']) && trim($_GET['title_debug'])!=''){
	$_SESSION['title_debug'] = $_GET['title_debug'];
	$title_debug = $_GET['title_debug'];
}
else {
	unset($_SESSION['title_debug']);
}
$xml = tmxTranslate('my-location.xml', REP_XLAT_TEMPLATE);
$xml_title = tmxTranslate('layoutFooter.xml', REP_XLAT_LAYOUT);
//**************Rented and Sold*****************//
if(isset($_POST['validate_rented'])){
	$id_location_map = $_POST['language_link'];
	modelLocationMap::updateValidateLocation($id_location_map,LOCATION_RENTED,null,'rented');
	redirectFrontHtml($_SERVER['HTTP_REFERER']);
}
if(isset($_POST['validate_sold'])){
	$id_location_map = $_POST['language_link'];
	modelLocationMap::updateValidateLocation($id_location_map,LOCATION_SELL,null,'sold');
	redirectFrontHtml($_SERVER['HTTP_REFERER']);
}
//****************************//
if(isset($_POST['invisible'])){
	if(modelLocationMap::getExistMyLocation($_SESSION['id_user'],$_POST['id_location'])){
		modelLocationMap::updateVisible($_POST['id_location'],0);
		//$msg = new Message($xml['error_message_1'], Message::AVERT);
   		Persistence::register(PERSIST_MESSAGE, $msg);
	}else{
		$msg = new Message($xml['error_message_6'], Message::AVERT);
   		Persistence::register(PERSIST_MESSAGE, $msg);
	}
}
if(isset($_POST['visible'])){
	if(modelLocationMap::getExistMyLocation($_SESSION['id_user'],$_POST['id_location'])){
		$user = modelUser::getProfil($_SESSION['id_user']);
		$count = modelLocationMap::countMyLocationVisible();
		if($user->total_credit > $count->count_credit){
			modelLocationMap::updateVisible($_POST['id_location'],1);
			//$msg = new Message($xml['error_message_3'], Message::AVERT);
	   		Persistence::register(PERSIST_MESSAGE, $msg);
		}else{
			$msg = new Message($xml['error_message_6'], Message::AVERT);
	   		Persistence::register(PERSIST_MESSAGE, $msg);
		}
	}else{
		$msg = new Message($xml['error_message_6'], Message::AVERT);
   		Persistence::register(PERSIST_MESSAGE, $msg);
	}
}
$myLocationByUrl = modelProvince::getProvinceByUrl('my-location.php');
	if($myLocationByUrl){
		$balise_title = $myLocationByUrl->title;
		$description = $myLocationByUrl->meta_title;
		$id_menu_left_tree = modelProvince::getProvinceMenuleft($myLocationByUrl->language_link);
		if($id_menu_left_tree){
			$id_menu_left_tree = modelProvince::getProvinceMenuleft($myLocationByUrl->language_link)->id_menu_left_tree;
		}else{$id_menu_left_tree = '';}
		
	}else{
		$balise_title = 'my_location_balise';
		$description = 'my_location_description';
	}
$page = new rootLayoutPage();
$page->setBaliseTitle($balise_title);
$page->setDescription($description);
/*$page->addStyleSheet('table_sorter.css');
$page->addJavaScript('jQuery.pagination.js');
$page->addStyleSheet('pagination.css');*/


$page->addJavaScript('flexigrid.js');
$page->addStyleSheet('flexigrid.css');
$page->addJavaScript('jQuery.jPopup.js');
$page->addStyleSheet('jPopup.css');
$page->addJavaScript('main.js');
//$page->setPageSameLevel($real_estate->id_menu_left_tree);

//-- menu Left --------------------------------//
$menu = new layoutMenuLeft();
$menuPage = new layoutMenuPage();
$menuPage->setTable('my-location');
$menuPage->setIdShow($id_menu_left_tree);
$menu->pushMenu($menuPage);
$page->setMenuPage($menu);

$header = new layoutHeader();
$header->setIdShow($id_menu_left_tree);
$page->setHeader($header);
//--------Menu indicator--------------//
$menuIndictor = new layoutMenuIndicator();
//$menuIndictor->idTree($real_estate->id_menu_left_tree);
//$menuIndictor->title($real_estate->title);
$page->setMenuIndicator($menuIndictor);

$tpl_file = 'my-location.tpl';
$Body = new flyLayout(REP_TPL . $tpl_file);
$Body->start();
	$Body->setVariable('url_ajax',REP_AJAX.'product/my_location_flexigrid_list.php?lang='.$_SESSION['language_code']);	
	$Body->setVariable('text_status',$xml_title['text_status']);
	$Body->setVariable('text_location',$xml_title['text_location']);
	//$Body->setVariable('text_price_sell',$xml_title['text_price_sell']);
	//$Body->setVariable('text_price_rental',$xml_title['text_price_rental']);
	//$Body->setVariable('text_city',$xml_title['text_city']);
	$Body->setVariable('text_visible',$xml['text_visible']);	
	$user = modelUser::getProfil($_SESSION['id_user']);
	$count = modelLocationMap::countMyLocationVisible();
	if($count and $user){
		if($count->count_credit >= $user->total_credit) {
			$Body->setVariable('credit', 0);
		}else $Body->setVariable('credit', ($user->total_credit-$count->count_credit));
		$Body->setVariable('all_credit', $user->total_credit);
	}else{
		$Body->setVariable('all_credit', '0');
	}
	
$Body->stop();
$page->setBody($Body);
$page->display();

?>